package org.eyeTopLight.service.impl;

import org.eyeTopLight.dao.FrameGoodsDao;
import org.eyeTopLight.po.FrameGoods;
import org.eyeTopLight.exception.ErrorCode;
import org.eyeTopLight.exception.ExceptionHandler;
import org.eyeTopLight.service.FrameGoodsService;
import org.eyeTopLight.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class FrameGoodsServiceImpl implements FrameGoodsService {

    public static final Logger logger = LoggerFactory.getLogger(FrameGoodsServiceImpl.class);

    @Resource
    FrameGoodsDao frameGoodsDao;

    @Override
    public FrameGoods getGoodsByPK(Integer goods_id) {

        FrameGoods searchGoods = new FrameGoods();
        searchGoods.setFrameGoods_id(goods_id);

        FrameGoods goods = null;
        try {
            goods = frameGoodsDao.queryByPK(searchGoods);
        }catch (Exception e){
            logger.error("查询数据库frameGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库goods表操作失败");
        }
        return goods;

    }

    @Override
    public List<GoodsVo> getGoods(String goods_category, Integer startIndex, Integer maxRows) {
        FrameGoods searchGoods = new FrameGoods();
        if(!goods_category.equals("全部")){
            searchGoods.setFrameGoods_category(goods_category);
        }
        List<GoodsVo> goodsList = new LinkedList<>();
        try{
            goodsList = frameGoodsDao.queryForPage(searchGoods,startIndex,maxRows);
        }
        catch (Exception e){
            logger.error("查询数据库frameGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库goods表操作失败");
        }
        return goodsList;
    }

    @Override
    public FrameGoods getOneGood(String goods_symbol, Integer goods_id) {
        FrameGoods frameGoods = null;

        FrameGoods searchGoods = new FrameGoods();
        searchGoods.setFrameGoods_id(goods_id);
        try{
            frameGoods = frameGoodsDao.queryByPK(searchGoods);
        }
        catch (Exception e){
            logger.error("查询数据库lenGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库lenGoods表操作失败");
        }
        return frameGoods;
    }

    @Override
    public List<GoodsVo> getTopNGoods(Integer count) {
        List<GoodsVo> topNFrames = frameGoodsDao.queryTopN(count);
        return topNFrames;
    }
}
